#include "pch.h"
#include "SeqStack.h"

void InitSeqStack(SeqStack *stack) {
	ElemType *p = (ElemType*)malloc(sizeof(ElemType)*STACK_INIT_SIZE);
	assert(p != NULL);
	stack->base = p;
	stack->top = 0;
	stack->capacity = STACK_INIT_SIZE;
}

bool isfull(SeqStack *stack) {
	return stack->top >= stack->capacity;
}

bool isempty(SeqStack *stack) {
	return stack->top == 0;
}

bool inc(SeqStack *stack) {
	ElemType *newbase = (ElemType*)realloc(stack->base, stack->capacity + STACK_INC_SIZE);
	assert(newbase != NULL);
	stack->base = newbase;
	stack->capacity += STACK_INC_SIZE;
	return true;
}

void push(SeqStack *stack,ElemType x) {
	if (isfull(stack)&&!inc(stack))
		return;
	stack->base[stack->top++] = x;
}

void pop(SeqStack *stack) {
	if (isempty(stack))
		return;

	stack->top--;
}

void gettop(SeqStack *stack, ElemType *v) {
	if (isempty(stack))
		return;

	*v = stack->base[stack->top - 1];
}

size_t length(SeqStack *stack) {
	return stack->top;
}

void clear(SeqStack *stack) {
	stack->top = 0;
}

void destroy(SeqStack *stack) {
	free(stack->base);
	stack->base = NULL;
	stack->capacity = stack->top = 0;
}

void show(SeqStack *stack) {
	for (size_t i = stack->top-1 ; i >= 0; i--)
	{
		printf("%d\n", stack->base[i]);
	}
}